<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>WebSocket client</title>
    <script type="text/javascript">
        var socket;
        if (typeof (WebSocket) == "undefined") {
            console.log("This explorer don't support WebSocket")
        }

        function connect() {
            //Connect WebSocket server
            socket = new WebSocket("ws://localhost:8080/chat/123");
            //open
            socket.onopen = function () {
                socket.send('{"userId":"1555855","message" : "test"}');
                socket.close("666666");
                console.log("数据发送中...");
                console.log("WebSocket is open");
            }
            //Get message
            socket.onmessage = function (msg) {
                console.log("Message is " + msg);
            }
            //close
            socket.onclose = function () {
                console.log("WebSocket is closed");
            }
            //error
            socket.onerror = function (e) {
                console.log("Error is " + e);
            }
        }

        function close() {
            socket.close();
        }

        function sendMsg() {
            socket.send("This is a client message ");
        }
    </script>
</head>

<body>
    <button onclick="connect()">connect</button>
    <button onclick="close()">close</button>
    <button onclick="sendMsg()">sendMsg</button>
</body>

</html>